﻿using Base.Repository.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Base.Repository.Instance
{
    public class MySqlRepository<T> : IRepository<T> where T : class, new()
    {
        private readonly DbContext _dbContext;

        public MySqlRepository(DbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 单个添加
        /// </summary>
        /// <param name="t"></param>
        public void Add(T t)
        {
            _dbContext.Add(t);
        }

        /// <summary>
        /// 多个添加
        /// </summary>
        /// <param name="ie"></param>
        public void AddRange(IEnumerable<T> ie)
        {
            _dbContext.AddRange(ie);
        }

        /// <summary>
        /// 单个删除
        /// </summary>
        /// <param name="t"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Delete(T t)
        {
            _dbContext.Remove(t);
        }

        /// <summary>
        /// 多个删除
        /// </summary>
        /// <param name="ie"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void DeleteRange(IEnumerable<T> ie)
        {
            _dbContext.RemoveRange(ie);
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> QueryAll(Expression<Func<T, bool>>? where=null)
        {
            IQueryable<T> ai = _dbContext.Set<T>();
            if (where != null)
                ai = ai.Where(where);

            return ai;
        }

        /// <summary>
        /// 查询排序
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="order"></param>
        /// <param name="isAsc"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, Expression<Func<T, bool>>? where = null)
        {
            var iq = this.QueryAll(where);
            return isAsc ? iq.OrderBy(order) : iq.OrderByDescending(order);
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        /// <typeparam name="type"></typeparam>
        /// <param name="total"></param>
        /// <param name="skip"></param>
        /// <param name="take"></param>
        /// <param name="order"></param>
        /// <param name="isAsc"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public IQueryable<T> QueryAll<type>(out int total, int skip, int take, Expression<Func<T, type>> order, bool isAsc = true, Expression<Func<T, bool>>? where = null)
        {
            if (skip < 0)
            {
                throw new Exception("错误");
            }
            var iq = this.QueryAll(order, isAsc, where);
            total = iq.Count();
            return iq.Skip(skip).Take(take);
        }

        /// <summary>
        /// 异步查询
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public Task<IQueryable<T>> QueryAllAsyanc(Expression<Func<T, bool>>? where)
        {
            return null;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public int SaveChanges()
        {
            return _dbContext.SaveChanges();
        }

        /// <summary>
        /// 单个修改
        /// </summary>
        /// <param name="t"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void Update(T t)
        {
            _dbContext.Entry<T>(t).State = EntityState.Modified;
        }

        /// <summary>
        /// 多个修改
        /// </summary>
        /// <param name="ie"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void UpdateRange(IEnumerable<T> ie)
        {
            foreach (var item in ie)
            {
                _dbContext.Entry<T>(item).State = EntityState.Modified;
            }
        }
    }
}
